<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解线程的join()方法</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
            color: #222;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            position: relative;
        }
        .code-header {
            background-color: #1e2227;
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
            padding: 8px 16px;
            display: flex;
            align-items: center;
        }
        .code-dots {
            display: flex;
            gap: 6px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .code-dot:nth-child(1) {
            background-color: #ff5f56;
        }
        .code-dot:nth-child(2) {
            background-color: #ffbd2e;
        }
        .code-dot:nth-child(3) {
            background-color: #27c93f;
        }
        .code-title {
            margin-left: 12px;
            color: #9da5b4;
            font-family: monospace;
        }
        pre {
            margin: 0;
            padding: 16px;
            overflow-x: auto;
        }
        code {
            font-family: 'Fira Code', monospace;
            color: #abb2bf;
        }
        .highlight {
            background-color: rgba(255, 215, 0, 0.2);
            padding: 2px 4px;
            border-radius: 4px;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .icon-box {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .mermaid {
            background-color: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-4xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">深入理解线程的 <span class="highlight">join()</span> 方法</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">掌握多线程编程中的关键同步机制</p>
            <div class="flex justify-center">
                <div class="inline-flex items-center bg-white bg-opacity-20 px-6 py-3 rounded-full">
                    <i class="fas fa-code text-lg mr-2"></i>
                    <span>Java 多线程核心技术</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="icon-box bg-blue-100 text-blue-600 mr-4">
                    <i class="fas fa-info-circle text-xl"></i>
                </div>
                <h2 class="text-2xl font-bold">方法概述</h2>
            </div>
            <p class="text-lg mb-4">线程的 <span class="font-semibold text-blue-600">join()</span> 方法用于控制线程的执行顺序，确保一个线程在另一个线程完成之前不会继续执行。</p>
            <p class="mb-6">具体来说，<span class="font-semibold">join()</span> 方法使当前线程等待直到调用 <span class="font-semibold">join()</span> 的线程完成（即终止）。这是线程间协调和控制的一种常用方式，特别是在需要确保某些操作在其他线程完成后才能继续进行的场景中。</p>
            
            <div class="grid md:grid-cols-2 gap-6 mt-8">
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-4 text-blue-600">核心功能</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span>控制线程执行顺序</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span>实现线程间同步</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mr-2 mt-1"></i>
                            <span>确保线程安全执行</span>
                        </li>
                    </ul>
                </div>
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-4 text-blue-600">适用场景</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-project-diagram text-purple-500 mr-2 mt-1"></i>
                            <span>依赖其他线程结果的操作</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tasks text-purple-500 mr-2 mt-1"></i>
                            <span>线程执行顺序敏感的任务</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-clock text-purple-500 mr-2 mt-1"></i>
                            <span>需要等待资源释放的场景</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Use Cases Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="icon-box bg-purple-100 text-purple-600 mr-4">
                    <i class="fas fa-list-ul text-xl"></i>
                </div>
                <h2 class="text-2xl font-bold">使用场景与效果</h2>
            </div>

            <div class="grid md:grid-cols-3 gap-6 mt-8">
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-check-double text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">确保线程完成</h3>
                    <p>当你启动了一个线程并希望当前线程在该线程完成之前等待，可以使用 <span class="font-semibold">join()</span> 方法。这样可以确保后续的操作在新线程执行完成后进行。</p>
                </div>
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-random text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">线程间的协调</h3>
                    <p>在多线程程序中，某些操作可能依赖于其他线程的结果。使用 <span class="font-semibold">join()</span> 方法可以确保这些操作在相关线程的计算结果可用后再执行。</p>
                </div>
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-sync-alt text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">实现线程同步</h3>
                    <p><span class="font-semibold">join()</span> 方法可以用于简单的同步场景，特别是在你需要将线程的执行顺序控制在一个特定的顺序时。</p>
                </div>
            </div>
        </section>

        <!-- Basic Usage Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="icon-box bg-green-100 text-green-600 mr-4">
                    <i class="fas fa-code text-xl"></i>
                </div>
                <h2 class="text-2xl font-bold">基本用法</h2>
            </div>

            <div class="code-block mb-8 shadow-lg">
                <div class="code-header">
                    <div class="code-dots">
                        <div class="code-dot"></div>
                        <div class="code-dot"></div>
                        <div class="code-dot"></div>
                    </div>
                    <div class="code-title">JoinDemo.java</div>
                </div>
                <pre><code class="language-java">public class JoinDemo {

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            System.out.println("线程 1 开始执行");
            try {
                Thread.sleep(1000); // 模拟长时间的操作
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            System.out.println("线程 1 完成");
        });

        Thread thread2 = new Thread(() -> {
            System.out.println("线程 2 开始执行");
            try {
                Thread.sleep(500); // 模拟长时间的操作
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            System.out.println("线程 2 完成");
        });

        thread1.start();
        thread2.start();

        try {
            thread1.join(); // 等待线程 1 完成
            thread2.join(); // 等待线程 2 完成
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        System.out.println("主线程完成");
    }
}</code></pre>
            </div>

            <div class="bg-blue-50 p-6 rounded-xl border border-blue-100">
                <h3 class="text-xl font-bold mb-4 text-blue-700">代码说明：</h3>
                <ul class="space-y-2">
                    <li class="flex items-start">
                        <span class="bg-blue-100 text-blue-700 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">1</span>
                        <span><span class="font-semibold">创建线程</span>：<code>thread1</code> 和 <code>thread2</code> 分别表示两个线程。</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-blue-100 text-blue-700 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">2</span>
                        <span><span class="font-semibold">启动线程</span>：使用 <code>start()</code> 方法启动线程。</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-blue-100 text-blue-700 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">3</span>
                        <span><span class="font-semibold">等待线程完成</span>：<code>main</code> 线程调用 <code>thread1.join()</code> 和 <code>thread2.join()</code>，使 <code>main</code> 线程等待 <code>thread1</code> 和 <code>thread2</code> 完成。</span>
                    </li>
                    <li class="flex items-start">
                        <span class="bg-blue-100 text-blue-700 rounded-full w-6 h-6 flex items-center justify-center mr-3 flex-shrink-0">4</span>
                        <span><span class="font-semibold">主线程完成</span>：在 <code>thread1</code> 和 <code>thread2</code> 完成后，<code>main</code> 线程继续执行，最后输出 "主线程完成"。</span>
                    </li>
                </ul>
            </div>
        </section>

        <!-- Variants Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="icon-box bg-yellow-100 text-yellow-600 mr-4">
                    <i class="fas fa-vial text-xl"></i>
                </div>
                <h2 class="text-2xl font-bold">方法变种</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-6">
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-yellow-600"><code>join(long millis)</code></h3>
                    <p class="mb-4">使当前线程等待指定的时间（毫秒）。如果在等待时间内线程完成，<code>join()</code> 会立即返回；如果时间到达仍未完成，<code>join()</code> 会在时间到达后返回。</p>
                    
                    <div class="code-block">
                        <pre><code class="language-java">thread1.join(1000); // 等待线程 1 最多 1000 毫秒</code></pre>
                    </div>
                </div>

                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-yellow-600"><code>join(long millis, int nanos)</code></h3>
                    <p class="mb-4">使当前线程等待指定的时间和纳秒。它的作用类似于 <code>join(long millis)</code>，但提供了更精确的时间控制。</p>
                    
                    <div class="code-block">
                        <pre><code class="language-java">thread1.join(1000, 500000); // 等待线程 1 最多 1000 毫秒和 500000 纳秒</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="icon-box bg-red-100 text-red-600 mr-4">
                    <i class="fas fa-project-diagram text-xl"></i>
                </div>
                <h2 class="text-2xl font-bold">线程执行流程可视化</h2>
            </div>

            <div class="mermaid">
                sequenceDiagram
                    participant M as 主线程
                    participant T1 as 线程1
                    participant T2 as 线程2

                    M->>T1: thread1.start()
                    M->>T2: thread2.start()
                    M->>T1: thread1.join()
                    T1-->>M: 线程1完成
                    M->>T2: thread2.join()
                    T2-->>M: 线程2完成
                    M->>M: System.out.println("主线程完成")
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-4xl px-4 text-center">
            <div class="mb-4">
                <p class="text-lg">技术小馆</p>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-300">
                    <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            },
            sequence: {
                diagramMarginX: 50,
                diagramMarginY: 10,
                boxTextMargin: 5,
                noteMargin: 10,
                messageMargin: 35,
                mirrorActors: false
            }
        });
    </script>
</body>
</html>